package cn.com.woxitech.dev.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.woxitech.dev.common.Const;
import cn.com.woxitech.dev.dao.gen.WmCatalogMapper;
import cn.com.woxitech.dev.dao.gen.WmCatalogRoomMapper;
import cn.com.woxitech.dev.dao.gen.WmCatalogUserMapper;
import cn.com.woxitech.dev.dao.gen.WmLecturePageMapper;
import cn.com.woxitech.dev.model.gen.WmApp;
import cn.com.woxitech.dev.model.gen.WmCatalog;
import cn.com.woxitech.dev.model.gen.WmCatalogExample;
import cn.com.woxitech.dev.model.gen.WmCatalogRoomExample;
import cn.com.woxitech.dev.model.gen.WmCatalogRoomKey;
import cn.com.woxitech.dev.model.gen.WmCatalogUserExample;
import cn.com.woxitech.dev.model.gen.WmCatalogUserKey;
import cn.com.woxitech.dev.model.gen.WmLecturePage;
import cn.com.woxitech.dev.model.gen.WmLecturePageExample;

@Service
public class CatalogService {
	@Autowired
	WmCatalogMapper wmCatalogMapper;

	@Autowired
	WmCatalogUserMapper wmCatalogUserMapper;
	
	@Autowired
	WmCatalogRoomMapper wmCatalogRoomMapper;
	
	public List<WmCatalog> getCatalogsByUuid(String userUuid) {
		// TODO Auto-generated method stub
		WmCatalogUserExample e = new WmCatalogUserExample();
		e.createCriteria().andUseruuuidEqualTo(userUuid);
		List<WmCatalogUserKey>wmCatalogUsers =  wmCatalogUserMapper.selectByExample(e);
		
		if(wmCatalogUsers.isEmpty())
			return new ArrayList<WmCatalog>();
		else{
			List<String> catalogStr = new ArrayList<String>();
			
			for(WmCatalogUserKey cu:wmCatalogUsers ){
				catalogStr.add(cu.getCataloguuid());
			}
			
			WmCatalogExample e2 = new WmCatalogExample();
			e2.createCriteria().andUuidIn(catalogStr);
			return wmCatalogMapper.selectByExample(e2);
		}
		
	}

	public List<String> getCatalogsStrByUuid(String userUuid) {
		// TODO Auto-generated method stub
		WmCatalogUserExample e = new WmCatalogUserExample();
		e.createCriteria().andUseruuuidEqualTo(userUuid);
		List<WmCatalogUserKey>wmCatalogUsers =  wmCatalogUserMapper.selectByExample(e);
		
		if(wmCatalogUsers.isEmpty())
			return new ArrayList<String>();
		else{
			List<String> catalogStr = new ArrayList<String>();
			
			for(WmCatalogUserKey cu:wmCatalogUsers ){
				catalogStr.add(cu.getCataloguuid());
			}
			return catalogStr;
		}
	}

	public List<String> getRoomUuidsByCatalogUuids(List<String> catalogUuidStrs) {
		// TODO Auto-generated method stub
		WmCatalogRoomExample e = new WmCatalogRoomExample();
		e.createCriteria().andCataloguuidIn(catalogUuidStrs);
		List<WmCatalogRoomKey> wmCatalogRooms = this.wmCatalogRoomMapper.selectByExample(e);
		
		if(wmCatalogRooms.isEmpty())
			return new ArrayList<String>();
		
		List<String> roomUUidStr = new ArrayList<String>();
		
		for(WmCatalogRoomKey cr:wmCatalogRooms ){
			roomUUidStr.add(cr.getRoomuuid());
		}
		
		
		return roomUUidStr;
	}

	public List<String> getRoomUuidsByOneCatalogUuid(String uuid) {
		// TODO Auto-generated method stub

		WmCatalogRoomExample e = new WmCatalogRoomExample();

		List<String> roomUUidStr = new ArrayList<String>();
		String hot = "994d1271-32b8-4cc6-937a-e54e449514bd";
		String latest = "ce6a8705-7e41-4983-b2fc-d04c33edc83c";
		if(uuid.equals(latest) || uuid.equals(hot)){
			e.createCriteria();;
		}else{
			e.createCriteria().andCataloguuidEqualTo(uuid);
		}
		
		List<WmCatalogRoomKey> wmCatalogRooms = this.wmCatalogRoomMapper.selectByExample(e);
		
		for(WmCatalogRoomKey cr:wmCatalogRooms ){
			roomUUidStr.add(cr.getRoomuuid());
		}
		
		
		return roomUUidStr;
	}

	public void insertCatalog_Room(String catalogUuid, String roomUuid) {
		// TODO Auto-generated method stub
		WmCatalogRoomKey c = new WmCatalogRoomKey();
		
		c.setCataloguuid(catalogUuid);
		c.setRoomuuid(roomUuid);
		this.wmCatalogRoomMapper.insert(c);
	}

	public void insertCatalog_User(String catalogUuid, String uuid) {
		// TODO Auto-generated method stub
		WmCatalogUserKey cu = new WmCatalogUserKey();
		cu.setCataloguuid(catalogUuid);
		cu.setUseruuuid(uuid);
		
		WmCatalogUserExample e = new WmCatalogUserExample();
		e.createCriteria().andCataloguuidEqualTo(catalogUuid).andUseruuuidEqualTo(uuid);
		
		int count = this.wmCatalogUserMapper.countByExample(e);
		
		if(count == 0 ){
			this.wmCatalogUserMapper.insert(cu);			
		}
		

	}


	public List<WmCatalog> getCatalogs(int appid) {
		// TODO Auto-generated method stub
		
		WmCatalogExample e = new WmCatalogExample();
		if(appid == 1){ 			
			e.createCriteria().andApp1EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else if(appid == 2){ 
			e.createCriteria().andApp2EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else if(appid == 3){ 			
			e.createCriteria().andApp3EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else if(appid == 4){ 
			e.createCriteria().andApp4EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else if(appid == 5){ 			
			e.createCriteria().andApp5EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else if(appid == 6){ 
			e.createCriteria().andApp6EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else if(appid == 7){ 			
			e.createCriteria().andApp7EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else if(appid == 8){ 
			e.createCriteria().andApp8EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else if(appid == 9){ 			
			e.createCriteria().andApp9EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else if(appid == 10){ 
			e.createCriteria().andApp10EqualTo(1);
			e.setOrderByClause(" ordernum");
			return this.wmCatalogMapper.selectByExample(e);
		}else{
			return new ArrayList<WmCatalog> ();
		}
		
	}

	public void deleteByRoomUuid(String uuid) {
		// TODO Auto-generated method stub
		
		WmCatalogRoomExample e = new WmCatalogRoomExample();
		e.createCriteria().andRoomuuidEqualTo(uuid);
		wmCatalogRoomMapper.deleteByExample(e);
	}

	public List<String> getCatalogUuidStrListByRoomUuid(String roomuuid) {
		// TODO Auto-generated method stub
		
		WmCatalogRoomExample e = new WmCatalogRoomExample();
		e.createCriteria().andRoomuuidEqualTo(roomuuid);
		List<WmCatalogRoomKey> crl=this.wmCatalogRoomMapper.selectByExample(e);
		
		List<String> l = new ArrayList<String>();
		
		for(WmCatalogRoomKey cr : crl){
			l.add(cr.getCataloguuid());
		}
		
		return l;
	}

	public void deleteByCatalogUUidAndRoomUuid(String c_uuid, String roomuuid) {
		// TODO Auto-generated method stub
		WmCatalogRoomExample example = new WmCatalogRoomExample();
		example.createCriteria().andCataloguuidEqualTo(c_uuid).andRoomuuidEqualTo(roomuuid);
		this.wmCatalogRoomMapper.deleteByExample(example);
	}

	public WmCatalog getCatalogByName(String name) {
		// TODO Auto-generated method stub
		WmCatalogExample e = new WmCatalogExample();
		e.createCriteria().andNameEqualTo(name);
		
		List<WmCatalog> l = this.wmCatalogMapper.selectByExample(e);
		
		if(l != null && !l.isEmpty()){
			return l.get(0);
		}else{
			return null;
		}
	}

	public WmCatalog getUndefaultCatalogByRoomUuid(String roomuuid) {
		// TODO Auto-generated method stub
		WmCatalogRoomExample e = new WmCatalogRoomExample();
		List<String> l = new ArrayList<String>();
		l.add(this.getCatalogByName(Const.CATALOG_ALL).getUuid());
		l.add(this.getCatalogByName(Const.CATALOG_HOT).getUuid());
		e.createCriteria().andRoomuuidEqualTo(roomuuid).andCataloguuidNotIn(l);
		
		List<WmCatalogRoomKey> crk = this.wmCatalogRoomMapper.selectByExample(e);
		
		if(crk == null || crk.isEmpty()){
			return null;
		}else{
			return this.wmCatalogMapper.selectByPrimaryKey(crk.get(0).getCataloguuid());
		}
	}

	public WmCatalog getCatalogByUuid(String uuid) {
		// TODO Auto-generated method stub
		return this.wmCatalogMapper.selectByPrimaryKey(uuid);
	}

	public void updateWmCatalog(WmCatalog catalog) {
		// TODO Auto-generated method stub
		this.wmCatalogMapper.updateByPrimaryKeySelective(catalog);
	}

	public void insertCatalog(WmCatalog catalog) {
		// TODO Auto-generated method stub
		this.wmCatalogMapper.insertSelective(catalog);
	}

	public void deleteByCatalogUuid(String uuid) {
		// TODO Auto-generated method stub
		this.wmCatalogMapper.deleteByPrimaryKey(uuid);
	}

	public void deleteCatalogRoomByCatalogUuid(String uuid) {
		// TODO Auto-generated method stub
		WmCatalogRoomExample e = new WmCatalogRoomExample();
		e.createCriteria().andCataloguuidEqualTo(uuid);
		this.wmCatalogRoomMapper.deleteByExample(e);
	}
}
